package com.lihepeng.leecode.juc;


import java.util.concurrent.Semaphore;

/**
 *
 */
class Resource04{
   private Semaphore twoSemaphore = new Semaphore(0);
    private Semaphore thirdSemaphore = new Semaphore(0);

    public void printOne() throws InterruptedException {
        System.out.println("one");
        twoSemaphore.release();

    }

    public void printTwo() throws InterruptedException {
        twoSemaphore.acquire();
        System.out.println("two");
        thirdSemaphore.release();
    }

    public void printThird() throws InterruptedException {
        thirdSemaphore.acquire();
        System.out.println("Third");
    }

}
public class PrintDemo4 {
    public static void main(String[] args) {
        Resource04 resource04 = new Resource04();
        new Thread(()->{
            while (true){
                try {
                    resource04.printOne();
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

        },"t1").start();
        new Thread(()->{
            while (true){
                try {
                    resource04.printTwo();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

        },"t2").start();
        new Thread(()->{
            while (true){
                try {
                    resource04.printThird();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

        },"t3").start();
    }
}
